// ==UserScript==
// @name           facebook - kttn-ify
// @version        1.0.0
// @description    Better than ignoring people, will turn all of their posts into random pictures of kittens.
// @namespace      http://scfs.me
// @author         snicker7

// @require        http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js

// @include        http://www.facebook.com/*
// @include        http://www.facebook.com/*home.php*
// @match          http://www.facebook.com/*

// @exclude        http://apps.facebook.com/*
// @exclude        http://facebook.com/apps/*
// ==/UserScript==

var tokttnify = new Array(
	"auebon.crosby"
);

var comments = new Array(
	"Look at these fucking kittens",
	"OMG kitties!",
	"mewwwww",
	"have you seen these kittens?",
	"Calm down, here are some kittens.",
	"kitties kitties kitties",
	"help, the cute is overwhelming!!!"
);

var generateKittenStory = function() {
	var image = $('<img class="img" src="http://www.facebook.com/images/loaders/indicator_blue_large.gif" />');
	var element = $('<div id="" data-ft="{&quot;type&quot;:&quot;attach&quot;}" class="UIStoryAttachment"></div>')
	element.append($('<div data-ft="{&quot;type&quot;:&quot;media&quot;}" class="UIStoryAttachment_Media UIStoryAttachment_MediaSingle"></div>')
		.append($('<div class="UIMediaItem UIMediaItem_Photo"></div')
			.append($('<a style="" target="" title="" id="" href="http://www.cutelittlekittens.com/" target="_blank"></a>')
				.append($('<div class="UIMediaItem_Wrapper"></div>')
					.append(image)
				)
			)
		)
	);
	element.append($('<div class="UIStoryAttachment_Info"></div>')
		.append($('<div class="UIStoryAttachment_Title"></div>')
			.append('<a target="_blank" href="http://www.cutelittlekittens.com/">Cute Little Kittens</a>')
		)
		.append($('<div class="UIStoryAttachment_Caption"></div>')
			.append("OMG KITTENS")
		)
	);
	getRandomKitty(image);
	return element;
}

var getRandomKitty = function(image) {
	GM_xmlhttpRequest({
		method: "GET",
		url: "http://cutelittlekittens.com/",
		onload: function(xhr) {
			if(xhr.readyState == 4) {
				var d = document.createElement('div');
				d.innerHTML = '<div id="root">' + xhr.responseText + '</div>';
				var imgsrc = $(d).find("table#AutoNumber5").find("img").eq(1).attr("src");
				$(image).attr("src",imgsrc);
				$(image).attr("height","75");
				$(image).parent().parent().attr("href",imgsrc);
			}
		},
	});
}

var changeStories = function() {
	for(var i=0;i<tokttnify.length;i++){
		var stories = $("div[id*=div_story]").filter(":has(.UIIntentionalStory_Names > a[href*="+tokttnify[i]+"])").filter(":not(.kttnd)");
		if(stories.length > 0) {
			stories.addClass("kttnd");
			stories.find(".UIStoryAttachment").remove();
			stories.each(function(i,item){
				$(item).find(".UIStory_Message").html(comments[Math.floor(Math.random()*comments.length)]);
				$(item).find(".UIIntentionalStory_Header").after(generateKittenStory());
			});
		}
	}
}

$(document).scroll(changeStories);
window.setInterval(changeStories,500);
changeStories();